package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.starseas_phone.Utils.AnimUtil;

import org.json.JSONObject;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {

    private Button Login_Stu_btn;
    private Button Login_Teacher_btn;

    private TextView Other_enter;
    private TextView Login_Stu_Forgot;
    private TextView Login_Teacher_Forgot;

    private EditText Login_Stu_user;
    private EditText Login_Stu_password;
    private EditText Login_Teacher_user;
    private EditText Login_Teacher_password;

    private LinearLayout Login_Stu;
    private LinearLayout Login_Teacher;

    String TAG = "LoginActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initview();
        // 学生登录
        Login_Stu_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Sto.State = 1;
                StuEnterMain();
            }
        });

        // 老师登录
        Login_Teacher_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Sto.State = 2;
                TeacherEnterMain();
            }
        });

        // 切换身份入口
        Other_enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Login_Teacher.getVisibility() == View.GONE) {
                    AnimUtil animUtil = new AnimUtil();
                    animUtil.FlipAnimatorXViewShow(Login_Stu,Login_Teacher,300);
                    Other_enter.setText("学生登录入口");
                } else {
                    AnimUtil animUtil = new AnimUtil();
                    animUtil.FlipAnimatorXViewShow(Login_Teacher,Login_Stu,300);
                    Other_enter.setText("教职工入口");
                }
            }
        });

        // 学生忘记密码
        Login_Stu_Forgot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,ForgotPwdActivity.class);
                intent.putExtra("id","Login");
                startActivity(intent);
            }
        });

        // 老师忘记密码
        Login_Teacher_Forgot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,ForgotPwdActivity.class);
                intent.putExtra("id","Login");
                startActivity(intent);
            }
        });
    }

    // 初始化控件
    private void initview(){
        Login_Stu_btn = (Button)findViewById(R.id.Login_Stu_btn);
        Login_Teacher_btn = (Button)findViewById(R.id.Login_Teacher_btn);

        Other_enter = (TextView) findViewById(R.id.Other_enter);
        Login_Stu_Forgot = (TextView) findViewById(R.id.Login_Stu_Forgot);
        Login_Teacher_Forgot = (TextView) findViewById(R.id.Login_Teacher_Forgot);

        Login_Stu_user = (EditText)findViewById(R.id.Login_Stu_user);
        Login_Stu_password = (EditText)findViewById(R.id.Login_Stu_password);
        Login_Teacher_user = (EditText)findViewById(R.id.Login_Teacher_user);
        Login_Teacher_password = (EditText)findViewById(R.id.Login_Teacher_password);

        Login_Stu = (LinearLayout)findViewById(R.id.Login_Stu);
        Login_Teacher = (LinearLayout)findViewById(R.id.Login_Teacher);

    }

    // 进入主界面
    private void TeacherEnterMain(){
        String TeaUsername = Login_Teacher_user.getText().toString();
        String TeaPassword = Login_Teacher_password.getText().toString();
        if (TeaUsername==null||TeaUsername.equals("")){
            Toast.makeText(LoginActivity.this,"账号不能为空!",Toast.LENGTH_SHORT).show();
        }else if (TeaPassword==null||TeaPassword.equals("")){
            Toast.makeText(LoginActivity.this,"密码不能为空!",Toast.LENGTH_SHORT).show();
        }else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        OkHttpClient client = new OkHttpClient.Builder()
                                .connectTimeout(60, TimeUnit.SECONDS)//设置连接超时时间
                                .readTimeout(60, TimeUnit.SECONDS)//设置读取超时时间
                                .build();
                        Request request = new Request.Builder()
                                .url(Sto.NetIP+"/api/Login?username="+TeaUsername+"&pwd="+TeaPassword+"&state="+Sto.State)
                                .build();
                        Response response = client.newCall(request).execute();
                        String a = response.body().string();
                        JSONObject jsonObject = new JSONObject(a);
                        if (jsonObject.getString("code").equals("200")){
                            JSONObject jsonObject1 = new JSONObject(jsonObject.getString("message"));
                            if (jsonObject1.getInt("PositionId")==6){
                                Sto.dept.setWorkerId(jsonObject1.getString("WorkerId"));
                                Sto.dept.setToken(jsonObject1.getString("Token"));
                                Sto.dept.setWorkerName(jsonObject1.getString("WorkerName"));
                                Sto.dept.setWorkerAge(jsonObject1.getInt("WorkerAge"));
                                Sto.dept.setWorkerbackTime(jsonObject1.getString("WorkerbackTime"));
                                Sto.dept.setWorkerCarid(jsonObject1.getString("WorkerCarid"));
                                Sto.dept.setWorkerEmail(jsonObject1.getString("WorkerEmail"));
                                Sto.dept.setWorkerLocal(jsonObject1.getString("WorkerLocal"));
                                Sto.dept.setWorkerPhone(jsonObject1.getString("WorkerPhone"));
                                Sto.dept.setWorkerSex(jsonObject1.getString("WorkerSex"));
                                Sto.dept.setPositionId(jsonObject1.getInt("PositionId"));
                                Sto.teachers.setPositionId(jsonObject1.getInt("PositionId"));
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(LoginActivity.this, Sto.dept.getWorkerName()+"老师,登录成功!",Toast.LENGTH_SHORT).show();
                                        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                                        startActivity(intent);
                                        finish();
                                    }
                                });
                            }else {
                                Sto.teachers.setWorkerId(jsonObject1.getString("WorkerId"));
                                Sto.teachers.setToken(jsonObject1.getString("Token"));
                                Sto.teachers.setWorkerName(jsonObject1.getString("WorkerName"));
                                Sto.teachers.setWorkerAge(jsonObject1.getInt("WorkerAge"));
                                Sto.teachers.setWorkerbackTime(jsonObject1.getString("WorkerbackTime"));
                                Sto.teachers.setWorkerCarid(jsonObject1.getString("WorkerCarid"));
                                Sto.teachers.setWorkerEmail(jsonObject1.getString("WorkerEmail"));
                                Sto.teachers.setWorkerLocal(jsonObject1.getString("WorkerLocal"));
                                Sto.teachers.setWorkerPhone(jsonObject1.getString("WorkerPhone"));
                                Sto.teachers.setWorkerSex(jsonObject1.getString("WorkerSex"));
                                Sto.teachers.setPositionId(jsonObject1.getInt("PositionId"));
                                Sto.teachers.setClassId(jsonObject1.getString("ClassId"));
                                Sto.teachers.setGrade(jsonObject1.getString("Grade"));
                                Sto.teachers.setCollegeName(jsonObject1.getString("CollegeName"));
                                Sto.teachers.setProfessionalName(jsonObject1.getString("ProfessionalName"));
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(LoginActivity.this, Sto.teachers.getWorkerName()+"老师,登录成功!",Toast.LENGTH_SHORT).show();
                                        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                                        startActivity(intent);
                                        finish();
                                    }
                                });
                            }

                        }else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(LoginActivity.this,"登录失败,账号密码错误!",Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    //学生进入主界面
    private void StuEnterMain(){
        String StuUsername = Login_Stu_user.getText().toString();
        String StuPassword = Login_Stu_password.getText().toString();
        if (StuUsername==null||StuUsername.equals("")){
            Toast.makeText(LoginActivity.this,"账号不能为空!",Toast.LENGTH_SHORT).show();
        }else if (StuPassword==null||StuPassword.equals("")){
            Toast.makeText(LoginActivity.this,"密码不能为空!",Toast.LENGTH_SHORT).show();
        }else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        OkHttpClient client = new OkHttpClient.Builder()
                                .connectTimeout(60, TimeUnit.SECONDS)//设置连接超时时间
                                .readTimeout(60, TimeUnit.SECONDS)//设置读取超时时间
                                .build();
                        Request request = new Request.Builder()
                                .url(Sto.NetIP+"/api/Login?username="+StuUsername+"&pwd="+StuPassword+"&state="+Sto.State)
                                .build();
                        Response response = client.newCall(request).execute();
                        String a = response.body().string();
                        JSONObject jsonObject = new JSONObject(a);
                        if (jsonObject.getString("code").equals("200")){
                            JSONObject jsonObject1 = new JSONObject(jsonObject.getString("message"));
                            Sto.students.setStuName(jsonObject1.getString("StuName"));
                            Sto.students.setStuAge(jsonObject1.getInt("StuAge"));
                            Sto.students.setStubackTime(jsonObject1.getString("StubackTime"));
                            Sto.students.setStuClassId(jsonObject1.getString("StuClassId"));
                            Sto.students.setStuEmail(jsonObject1.getString("StuEmail"));
                            Sto.students.setStuPhone(jsonObject1.getString("StuPhone"));
                            Sto.students.setStuSex(jsonObject1.getString("StuSex"));
                            Sto.students.setStuId(jsonObject1.getString("StuId"));
                            Sto.students.setStuId(jsonObject1.getString("StuId"));
                            Sto.students.setStuCarid(jsonObject1.getString("StuCarid"));
                            Sto.students.setGrade(jsonObject1.getString("Grade"));
                            Sto.students.setTheCounselorId(jsonObject1.getString("TheCounselorId"));
                            Sto.students.setTheCounselorName(jsonObject1.getString("TheCounselorName"));
                            Sto.students.setTheCounselorPhone(jsonObject1.getString("TheCounselorPhone"));
                            Sto.students.setPoliticalLandscape(jsonObject1.getString("PoliticalLandscape"));
                            Sto.students.setNativePlace(jsonObject1.getString("NativePlace"));
                            Sto.students.setNational(jsonObject1.getString("National"));
                            Sto.students.setProfessionalName(jsonObject1.getString("ProfessionalName"));
                            Sto.students.setCollegeName(jsonObject1.getString("CollegeName"));
                            Sto.students.setToken(jsonObject1.getString("Token"));
                            Sto.students.setStateOfTheSchool(jsonObject1.getString("StateOfTheSchool"));

//                            StuStorage.StuId = jsonObject1.getString("StuId");
//                            StuStorage.Token = jsonObject1.getString("Token");
//                            StuStorage.StuName = jsonObject1.getString("StuName");
//                            StuStorage.StuClassId = jsonObject1.getString("StuClassId");
//                            StuStorage.StuSex = jsonObject1.getString("StuSex");
//                            StuStorage.CollegeName = jsonObject1.getString("CollegeName");
//                            StuStorage.ProfessionalName = jsonObject1.getString("ProfessionalName");
//                            StuStorage.Grade = jsonObject1.getString("Grade");
//                            StuStorage.StuAge = jsonObject1.getInt("StuAge");
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(LoginActivity.this, Sto.students.getStuName()+"同学,登录成功!",Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            });
                        }else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(LoginActivity.this,"登录失败,账号密码错误!",Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

}